package com.emep.changzhi.analyse.service.basics;


import com.emep.changzhi.analyse.entity.basics.SysEmployee;
import com.emep.changzhi.analyse.entity.basics.SysLeadershipInfo;
import com.emep.changzhi.analyse.entity.basics.SysMonitorSiteInfo;
import com.emep.changzhi.analyse.repository.basics.SysEmployeeRepository;
import com.emep.changzhi.analyse.utils.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysEmployeeService {

    @Resource
    private SysEmployeeRepository repository;
    private static final Logger logger = LoggerFactory.getLogger(SysEmployeeService.class);

    /**
     *
     * 新增系统员工信息
     *
     * @param person
     * @return
     * @throws Exception
     */
    public boolean save(SysEmployee person) {
        List<SysEmployee> infos =repository.findAllByName(person.getName());
        if(infos.isEmpty()){
            repository.save(person);
            return true;
        }
        return false;
    }



    /**
     * 修改
     *
     * @param info
     * @return
     */
    public boolean update(SysEmployee info) {
        List<SysEmployee> infos =repository.findAllByNameAndIdNot(info.getName(),info.getId());
        if(infos.isEmpty()){
            repository.save(info);
            return true;
        }
        return false;
    }


    /**
     *
     * 根据id 删除系统员工信息
     *
     * @param id
     */
    public boolean delete(Integer id) {
        boolean result = false;
        try {
            repository.deleteById(id);
           result =true;
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return result;
    }
    /**
     *查询部门所有员工信息
     *
     * @return
     */
    public List<SysEmployee> findAllByDepartment(int id) {
        List<SysEmployee> personList = null;
        try {
            personList =  repository.findAllByDeppartid(id);
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return personList;
    }

    /**
     *
     *
     * 根据id查询系统员工信息
     *
     * @param id
     * @return
     */
    public SysEmployee findById(int id){
        SysEmployee regionInfo = null;
        try {
            regionInfo = repository.findById(id);
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return regionInfo;
    }

    public Page<SysEmployee> find(int pageIndex, int pageSize, Map<String, Object> params) {

        Page<SysEmployee> industryPage = repository.findAll(new SysEmployeeService.SysEmployeeSpec(params),
                PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC, "id"));
        return industryPage;
    }


    private class SysEmployeeSpec implements Specification<SysEmployee> {
        private Map<String, Object> params = new HashMap<String, Object>();

        public SysEmployeeSpec(Map<String, Object> params) {
            this.params = params;
        }

        @Override
        public Predicate toPredicate(Root<SysEmployee> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> lstPredicates = new ArrayList<Predicate>();

            Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
            return cb.and(lstPredicates.toArray(arrayPredicates));
        }
    }



}
